package pl.myszon.RILLib;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Myszon
 */
class StatusParser extends ResponseParser {
    Response response;
    
    @Override
    ServerAnswer<Map> parse(Response resp) {
        response = resp;
        StatusCode status = super.parseStatusCode(response);
        Map<String, Integer> limitInfo = new HashMap<String, Integer>();

        Integer value = getKeyValue("X-Limit-User-Limit");
        limitInfo.put("User-Limit", value);

        value = getKeyValue("X-Limit-User-Remaining");
        limitInfo.put("User-Remaining", value);

        value = getKeyValue("X-Limit-User-Reset");
        limitInfo.put("User-Reset", value);

        value = getKeyValue("X-Limit-Key-Limit");
        limitInfo.put("Key-Limit", value);

        value = getKeyValue("X-Limit-Key-Remaining");
        limitInfo.put("Key-Remaining", value);

        value = getKeyValue("X-Limit-Key-Reset");
        limitInfo.put("Key-Reset", value);

        return new ServerAnswer<Map>(status, limitInfo);
    }

    private int getKeyValue(String key) {
        List resp = (List) response.headers.get(key);
        String header = (String) resp.get(0);
        return Integer.parseInt(header);
    }
}
